package com.itheima.appserver.controller;

import com.itheima.appserver.interceptor.UserHandler;
import com.itheima.appserver.service.UserInfoService;
import com.itheima.model.domain.Counts;
import com.itheima.model.domain.UserInfo;
import com.itheima.model.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UserInfoService userInfoService;

    @GetMapping
    public ResponseEntity findByid(Long userId, @RequestHeader("Authorization") String token) {
        //设置userinfo的id

        //判断
        if (userId == null) {
            userId = UserHandler.getId();
        }
        UserInfoVo userInfo = userInfoService.findById(userId);
        return ResponseEntity.ok(userInfo);
    }

    @PutMapping
    public ResponseEntity updateUser(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token) {
        //设置userinfo的id
        //3.设置用户id
        userInfo.setId(UserHandler.getId());
        userInfoService.updateUser(userInfo);
        return ResponseEntity.ok(null);
    }

    //喜欢统计
    @GetMapping("/counts")
    public ResponseEntity counts() {
        Counts counts = userInfoService.coutns();
        return ResponseEntity.ok(counts);
    }
}
